package cn.iocoder.yudao.module.diy.service.diy;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.diy.controller.admin.diy.vo.other.StartUpPageConfigParam;
import cn.iocoder.yudao.module.diy.controller.admin.diy.vo.other.StartUpPageConfigVo;
import cn.iocoder.yudao.module.diy.dal.dataobject.addon.AddonDO;
import cn.iocoder.yudao.module.diy.dal.diy.BottomConfigValue;
import cn.iocoder.yudao.module.diy.dal.diy.BottomConfigVo;
import cn.iocoder.yudao.module.diy.dal.mysql.addon.AddonMapper;
import cn.iocoder.yudao.module.diy.exception.AdminException;
import cn.iocoder.yudao.module.diy.loader.JsonModuleLoader;
import cn.iocoder.yudao.module.diy.pojo.Addon;
import cn.iocoder.yudao.module.diy.service.config.DiyConfigService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CoreDiyConfigService implements ICoreDiyConfigService {


    @Resource
    DiyConfigService diyConfigService;
    @Autowired
    private AddonMapper addonMapper;

    private static JSONArray bottomJson;

    private static JSONArray getBottomJson() {
        if (bottomJson == null||bottomJson.size()==0) {
            JsonModuleLoader jsonModuleLoader = new JsonModuleLoader();
            bottomJson = jsonModuleLoader.mergeContentToArray("diy/bottom.json");
        }
        return bottomJson;
    }

    @Override
    public List<BottomConfigVo> getBottomList() {
        JSONArray bottomJson = getBottomJson();

        List<BottomConfigVo> list = new ArrayList<>();
        for (Object bottomMap : bottomJson) {
            JSONObject jsonObject = (JSONObject) bottomMap;
            BottomConfigVo vo = new BottomConfigVo();
            String key = jsonObject.getStr("key");

            AddonDO addon = new AddonDO();
            if(key.equals("app"))
            {
                addon.setTitle("默认");
            }else {
                addon = addonMapper.selectOne(new QueryWrapper<AddonDO>().eq("`addon_key`", key));
            }

            vo.setInfo(addon);
            vo.setKey(key);
            JSONObject bottomValueJson = jsonObject.getJSONObject("value");
            BottomConfigValue bottomConfigValue = JSONUtil.toBean(bottomValueJson, BottomConfigValue.class);
            vo.setValue(bottomConfigValue);
            list.add(vo);
        }

        return list;
    }

    @Override
    public BottomConfigVo getBottomConfig(Long tenantId, String key) {
        List<BottomConfigVo> list = getBottomList();

        BottomConfigVo config = new BottomConfigVo();
        for (BottomConfigVo vo : list)
        {
            if(vo.getKey().equals(key))
                config = vo;
        }
        if(config.getKey().equals(""))
        {
            throw new AdminException("未找到该配置");
        }
        String configKey = "diy_bottom_" + key;
        JSONObject jsonObject = diyConfigService.getConfigValue( configKey);
        if(ObjectUtil.isNotEmpty(jsonObject))
        {
            BottomConfigValue bottomConfigValue = JSONUtil.toBean(jsonObject, BottomConfigValue.class);
            config.setValue(bottomConfigValue);

        }

        return config;
    }

    /**
     * 设置底部导航
     *
     * @param tenantId
     * @param bottomConfigValue
     * @param key
     */
    @Override
    public void setBottomConfig(Long tenantId,Long siteId, BottomConfigValue bottomConfigValue, String key) {
        diyConfigService.setConfig(TenantContextHolder.getTenantId(),1l,siteId ,"diy_bottom_" + key, JSONUtil.parseObj(bottomConfigValue));
    }

    /**
     * 启动页配置
     *
     * @param value
     * @param name
     */
    @Override
    public void setStartUpPageConfig(Long tenantId,Long addonId,Long siteId, StartUpPageConfigParam value, String name) {
        diyConfigService.setConfig(tenantId,addonId,siteId,"START_UP_PAGE_" + name.toUpperCase(), JSONUtil.parseObj(value));
    }

    /**
     * 获取启动页配置
     *
     * @param tenantId
     * @param name
     * @return
     */
    public StartUpPageConfigVo getStartUpPageConfig(Long tenantId, String name) {
        JSONObject config =diyConfigService.getConfigValue("START_UP_PAGE_" + name.toUpperCase());
        if (config.isEmpty()) return null;
        return JSONUtil.toBean(config, StartUpPageConfigVo.class);
    }
}
